﻿using System.Text.Json;

using SkiaSharp;

using XServer.Models.Http;

namespace XServer.Utility
{
    public class PictureHelper
    {
        /// <summary>
        /// 高性能图片压缩
        /// </summary>
        /// <param name="filePath">图片路径</param>
        /// <param name="desiredWidth">输出宽</param>
        /// <param name="desiredHeight">输出长</param>
        public static byte[] GenerateThumbnail(string filePath, int desiredWidth, int desiredHeight)
        {
            // 假设imagePath是网络可访问路径或从数据库等获取图像数据的方法
            // 这里简化处理，实际应用中可能需要根据实际情况读取图片数据

            byte[] imageBytes;  // 假设这是从某个地方读取到的图片字节数组

            // 示例：从本地文件加载图片字节数组（生产环境中请替换为适合的方式）
            imageBytes = System.IO.File.ReadAllBytes(filePath);

            using MemoryStream inputStream = new MemoryStream(imageBytes);
            using SKBitmap originalImage = SKBitmap.Decode(inputStream);

            float scale = Math.Max(desiredWidth / (float)originalImage.Width, desiredHeight / (float)originalImage.Height);
            int newWidth = (int)(originalImage.Width * scale);
            int newHeight = (int)(originalImage.Height * scale);

            using SKBitmap thumbnail = new SKBitmap(newWidth, newHeight);
            using SKCanvas canvas = new SKCanvas(thumbnail);

            canvas.Clear(SKColors.White); // 可选：设置背景颜色
            canvas.Scale(scale);
            canvas.DrawBitmap(originalImage, 0, 0, new SKPaint { FilterQuality = SKFilterQuality.High });

            using SKImage image = SKImage.FromBitmap(thumbnail);
            using SKData data = image.Encode(SKEncodedImageFormat.Jpeg, quality: 90);

            return data.ToArray();
        }

        public static byte[] GenerateThumbnailNew(string filePath, int desiredWidth, int desiredHeight)
        {
            // 假设imagePath是网络可访问路径或从数据库等获取图像数据的方法
            // 这里简化处理，实际应用中可能需要根据实际情况读取图片数据

            byte[] imageBytes;  // 假设这是从某个地方读取到的图片字节数组

            // 示例：从本地文件加载图片字节数组（生产环境中请替换为适合的方式）
            imageBytes = System.IO.File.ReadAllBytes(filePath);

            using MemoryStream inputStream = new MemoryStream(imageBytes);
            using SKBitmap originalImage = SKBitmap.Decode(inputStream);

            float scale = Math.Max(desiredWidth / (float)originalImage.Width, desiredHeight / (float)originalImage.Height);
            int newWidth = (int)(originalImage.Width * scale);
            int newHeight = (int)(originalImage.Height * scale);

            using SKBitmap thumbnail = new SKBitmap(newWidth, newHeight);
            using SKCanvas canvas = new SKCanvas(thumbnail);

            using SKImage img = SKImage.FromBitmap(originalImage);
            var samplingOptions = new SKSamplingOptions(SKCubicResampler.Mitchell);
            var paint = new SKPaint
            {
                IsAntialias = true
            };

            canvas.Clear(SKColors.White); // 可选：设置背景颜色
            canvas.Scale(scale);
            canvas.DrawImage(img, 0, 0, samplingOptions, paint);

            using SKImage image = SKImage.FromBitmap(thumbnail);
            using SKData data = image.Encode(SKEncodedImageFormat.Jpeg, quality: 90);

            return data.ToArray();
        }
    }
}
